package net.sf.rose.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.rose.exception.SystemErrorCode;
import net.sf.rose.exception.SystemException;

import org.apache.commons.lang.StringUtils;

public class DataUtil {

	public static Class<?> getClass(String classname) {
		try {
			if (classname != null) {
				return Class.forName(classname);
			}
		} catch (Exception e) {
			throw new SystemException("", SystemErrorCode.CONNECT_CLOSE_ERROR);
		}
		return null;
	}

	public static Object getInstance(String classname) {
		try {
			if (classname != null) {
				return Class.forName(classname).newInstance();
			}
		} catch (Exception e) {
			throw new SystemException("", SystemErrorCode.CONNECT_CLOSE_ERROR);
		}
		return null;
	}
	
	public static Object getClone(Object source) throws Exception {
		// 读对象，反序列化
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		ObjectOutputStream out = new ObjectOutputStream(byteOut);
		out.writeObject(source);
		// 写对象，序列化
		ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
		ObjectInputStream in = new ObjectInputStream(byteIn);
		return in.readObject();
	}

	/**
	 * 判断二个值是否相等，包括空值
	 */
	public static boolean equals(Object o1, Object o2) {
		if (o1 == null) {
			return o2 == null ? true : false;
		} else {
			return o1.equals(o2);
		}
	}

	/**
	 * 判断参数是否为空，为空字符串，为"null"字符串。
	 * 
	 * @param args
	 */
	public static boolean isNullEmpty(Object arg) {
		if (arg == null) {
			return true;
		} else if (arg.equals("")) {
			return true;
		} else if (arg.equals("null")) {
			return true;
		}
		return false;
	}

	/**
	 * 与isNullEmpty方法返回值相反
	 * 
	 * @param args
	 */
	public static boolean isNotNullEmpty(Object arg) {
		return !isNullEmpty(arg);
	}

	/**
	 * @param args
	 */
	public static boolean isNumber(Object arg) {
		if (isNullEmpty(arg)) {
			return false;
		}

		try {
			parseNumber(clearNumber(arg));
		} catch (RuntimeException e) {
			return false;
		}
		return true;
	}

	/**
	 * clear \t and ' and " and space for string
	 * 
	 * @return String
	 */
	public static String clearMark(String string) {
		string = string.replace('\t', ' ');
		string = string.replace('\'', ' ');
		string = string.replace('\"', ' ');
		string = StringUtils.deleteWhitespace(string);
		return string;
	}

	/**
	 * clear \t and ' and " and space for string
	 * 
	 * @return String
	 */
	public static String clearNumber(Object val) {
		String str = val.toString();
		str = StringUtils.deleteWhitespace(str);
		str = StringUtils.remove(str, ',');
		str = StringUtils.remove(str, '$');
		return str;
	}

	/**
	 * 使用正则表达式判断参数是否是标准的邮件地址格式。
	 */
	public static boolean isEmail(String string) {
		if (string == null)
			return false;
		String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		Pattern p;
		Matcher m;
		p = Pattern.compile(regEx1);
		m = p.matcher(string);
		if (m.matches())
			return true;
		else
			return false;
	}

	/**
	 * 
	 */
	public static java.sql.Date getSqlDate(java.util.Date date) {
		return new java.sql.Date(date.getTime());
	}

	/**
	 * 
	 */
	public static Byte getByte(Object obj) {
		return Byte.valueOf(obj.toString());
	}

	/**
	 * 
	 */
	public static byte getbyte(Object obj) {
		return Byte.parseByte(obj.toString());
	}

	/**
	 * 
	 */
	public static Integer getInt(Object obj) {
		return Integer.valueOf(obj.toString());
	}

	public static int getint(Object obj) {
		return Integer.parseInt(obj.toString());
	}

	/**
	 * 
	 */
	public static Short getShort(Object obj) {
		return Short.valueOf(obj.toString());
	}

	/**
	 * 
	 */
	public static short getshort(Object obj) {
		return Short.parseShort(obj.toString());
	}

	/**
	 * 
	 */
	public static Long getLong(Object obj) {
		return Long.valueOf(obj.toString());
	}

	/**
	 * 
	 */
	public static long getlong(Object obj) {
		return Long.parseLong(obj.toString());
	}

	/**
	 * 
	 */
	public static Double getDouble(Object obj) {
		return Double.valueOf(obj.toString());
	}

	/**
	 * 
	 */
	public static double getdouble(Object obj) {
		return Double.parseDouble(obj.toString());
	}

	/**
	 * 
	 */
	public static Float getFloat(Object obj) {
		return Float.valueOf(obj.toString());
	}

	/**
	 * 
	 */
	public static float getfloat(Object obj) {
		return Float.parseFloat(obj.toString());
	}

	/**
	 * 
	 */
	public static String getString(Object obj) {
		return obj.toString();
	}

	/**
	 * 
	 */
	public static Boolean getBoolean(Object obj) {
		return Boolean.valueOf(obj.toString());
	}

	/**
	 * 
	 */
	public static boolean getboolean(Object obj) {
		return Boolean.getBoolean(obj.toString());
	}

	/**
	 * date methods block end.
	 */

	public static String formatNumber(long l) {
		return NumberFormat.getInstance().format(l);
	}

	/**
	 * 
	 */
	public static String formatNumber(double d) {
		return NumberFormat.getInstance().format(d);
	}

	/**
	 * 
	 */
	public static String formatNumber(Object o) {
		if (o.toString().indexOf('.') != -1) {
			return NumberFormat.getInstance().format(getdouble(o));
		} else {
			return NumberFormat.getInstance().format(getlong(o));
		}
	}

	/**
	 * 
	 */
	public static String formatNumber(Object o, String pattern) {
		if (o.toString().indexOf('.') != -1) {
			return new DecimalFormat(pattern).format(getdouble(o));
		} else {
			return new DecimalFormat(pattern).format(getlong(o));
		}
	}

	/**
	 * 
	 */
	public static String formatCurrency(long l) {
		return NumberFormat.getCurrencyInstance().format(l);
	}

	/**
	 * 
	 */
	public static String formatCurrency(Object o) {
		return NumberFormat.getCurrencyInstance().format(getlong(o));
	}

	/**
	 * 
	 */
	public static String formatPercent(double o) {
		return NumberFormat.getPercentInstance().format(o);
	}

	/**
	 * 
	 */
	public static String formatPercent(Object o) {
		return NumberFormat.getPercentInstance().format(getdouble(o));
	}

	/**
	 * 
	 */
	public static Object parseNumber(Object o) {
		try {
			return NumberFormat.getInstance().parseObject(o.toString());
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	public static void main(String[] args) {
		System.out.println(formatCurrency("12232"));
		System.out.println(formatPercent("12232.90"));
		System.out.println(formatNumber("12232.90"));
		System.out.println(parseNumber("1244").getClass());
		System.out.println(parseNumber("1277777777444.888").getClass());
		System.out.println(formatNumber("22.90", "##,###,###.000"));
		System.out.println(formatNumber(new Double("12232.90")));

		System.out.println(StringUtils.deleteWhitespace("aa  aa		aa"));
	}
}
